//
//  hd_fs_operation_test.cpp
//  Draco
//
//  Created by Liu Dafan on 12-2-24.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#include "common.h"
#include "data_operations.h"
#include "hd_fs_operation.h"

using namespace std;
static Config *conf = NULL;
void test_init()
{
    conf = readconf("dp.conf");

    // Initialize
    DataOperations::Init(conf);    
}

void test_uninit()
{
    DataOperations::UnInit();
    delete conf;
    conf = NULL;
}

int get_real_count(const vector<DIR_INFO>& infos)
{
    int count = 0;
    for (size_t idx = 0; idx < infos.size(); idx++) {
        if (!infos[idx]._isDeleted)
        {
            count ++;
        }
    }
    
    return count;
}

int get_real_count(const vector<FILE_INFO>& infos)
{
    int count = 0;
    for (size_t idx = 0; idx < infos.size(); idx++) {
        if (!infos[idx]._isDeleted)
        {
            count ++;
        }
    }
    
    return count;
}

void dir_operation_test()
{
    HadoopFSOperation *hdoperation = new HadoopFSOperation("dirtable", "filetable");
    // create dir
    DIR_ID root;
    root._id = 0;
    DIR_INFO dirInfo;
    string dirName = "test_dir";
    string newDirName = "new_test_dir";
    HResult ret = hdoperation->CreateNewDir(root, dirName, dirInfo);
    vector<DIR_INFO> dirinfos;
    
    // judge
    if (ret != ERROR_CODE_OK)
    {
        printf("Create Dir failed due to err:%d\n", ret);
        return;
    }
    ret = hdoperation->GetSubdirInfos(root, dirinfos);
    if (ret != ERROR_CODE_OK)
    {
        printf("Get Sub Dir Info failed due to err:%d\n", ret);
        return;
    }

    if (get_real_count(dirinfos) != 1)
    {
        printf("Dir Size is not correct: %d\n", get_real_count(dirinfos));
        return;
    }
    //TODO: compare DirInfo
    
    printf("pass create dir test\n");
    // rename dir
    ret = hdoperation->RenameDir(root, dirName, newDirName, dirInfo);
    if (ret != ERROR_CODE_OK)
    {
        printf("Create Dir failed due to err:%d\n", ret);
        return;
    }
    printf("pass rename dir test\n");
    
    // delete dir
    ret = hdoperation->DeleteDir(root, newDirName);
    
    // judge
    if (ret != ERROR_CODE_OK)
    {
        printf("Delete Dir failed due to err:%d\n", ret);
        return;
    }
    ret = hdoperation->GetSubdirInfos(root, dirinfos);
    if (ret != ERROR_CODE_OK)
    {
        printf("Get Sub Dir Info failed due to err:%d\n", ret);
        return;
    }

    if (get_real_count(dirinfos) != 0)
    {
        printf("Dir Size is not correct: %d\n", get_real_count(dirinfos));
        return;
    }
    
    printf("pass delete dir test\n");
    delete hdoperation;
}

void file_operation_test()
{
    HadoopFSOperation *hdoperation = new HadoopFSOperation("dirtable", "filetable");
    // create dir
    DIR_ID root;
    root._id = 0;
    FILE_INFO fileInfo;
    const char* testdata = "hello world";
    string fileName = "test_file";
    string newFileName = "new_test_file";
    HResult ret = hdoperation->CreateNewFile(root, fileName, (const int8_t*)testdata, strlen(testdata), fileInfo);
    vector<FILE_INFO> fileinfos;
    
    // judge
    if (ret != ERROR_CODE_OK)
    {
        printf("Create File failed due to err:%d\n", ret);
        return;
    }
    ret = hdoperation->GetSubfileInfos(root, fileinfos);
    if (ret != ERROR_CODE_OK)
    {
        printf("Get Sub File Info failed due to err:%d\n", ret);
        return;
    }
    
    if (get_real_count(fileinfos) != 1)
    {
        printf("File Size is not correct: %d\n", get_real_count(fileinfos));
        return;
    }
    //TODO: compare DirInfo
    
    printf("pass create file test\n");
    
    // read file
    char buf[1024];
    size_t length;
    ret = hdoperation->ReadFile(fileinfos[0], 0, (int8_t*)buf, strlen(testdata), length);
    if (ret != ERROR_CODE_OK)
    {
        printf("Read File data failed due to err:%d\n", ret);
        return;
    }
    
    if (strncmp(buf, testdata, length) != 0)
    {
        printf("Data mismatch, old:%s, new:%s\n",
               testdata, buf);
        return;
    }
    
    printf("pass read file test\n");
    
    // rename file
    ret = hdoperation->RenameFile(root, fileName, newFileName, fileInfo);
    if (ret != ERROR_CODE_OK)
    {
        printf("Rename file failed due to err:%d\n", ret);
        return;
    }
    printf("pass rename file test\n");
    
    // delete file
    ret = hdoperation->DeleteFile(root, newFileName);
    
    // judge
    if (ret != ERROR_CODE_OK)
    {
        printf("Delete File failed due to err:%d\n", ret);
        return;
    }
    ret = hdoperation->GetSubfileInfos(root, fileinfos);
    if (ret != ERROR_CODE_OK)
    {
        printf("Get Sub File Info failed due to err:%d\n", ret);
        return;
    }
    
    if (get_real_count(fileinfos) != 0)
    {
        printf("File Size is not correct: %d\n", get_real_count(fileinfos));
        return;
    }
    
    printf("pass delete file test\n");
    delete hdoperation;
}
